#pragma once
#include "TcpServer.hpp"
#include "HttpProtocol.hpp"
#include "Session.hpp"
namespace HttpServerModule
{
    using namespace TcpserverModule;
    class HttpServer
    {
    public:
        HttpServer(int port)
            : _tsvr(std::make_shared<TcpServer>(port))
        {
            _tsvr->RegisterHandler([this](SockPtr ret, InetAddr client)
                                   { return this->Handler(ret, client); });
        }
        void Start()
        {
            _tsvr->Loop();
        }

        void Handler(SockPtr ret, InetAddr client)
        {
            auto _manager = SessionManager::GetInstance();
            LOG(LogLevel::INFO) << "new client fd: " << ret->FD() << "client info: " << client.Addr();
            // 接受数据
            std::string http_str;
            ret->TcpRecv(&http_str);
            
            // std::cout << "--------------------------------" << std::endl;
            //  std::cout << http_str << std::endl;
            // std::cout << "--------------------------------" << std::endl;

            Request req;
            Response res;
            //浏览器输入 http://39.105.158.135:8080/login?username=123123 进入登录
            //反序列化
            req.Deserialize(http_str);
            // req.Print();

            req.Parse(); //解析登录成功后浏览器添加的cookie报头

            if (req.Url() == "/login") //进入登录 (GET 后添加参数)
            {
                std::string sessionid = req.GetSessionId();
                if (sessionid.empty()) //说明此时还没有用户登录呢?
                {
                    //进行硬编码
                    static int number = 0;
                    std::string username = "user-" + std::to_string(number++);
                    session_ptr s = std::make_shared<Session>(username, "Logined");
                    std::string sessionid = _manager->AddSession(s); //添加用户
                    // std::cout << "manager addr: " << &_manager << std::endl; //一样的啊

                    res.AddHeader("Set-Cookie: sessionid=" + sessionid +";"); //添加响应报头
                    std::cout  << "用户: " << s->_username << "被添加, sessionid是: " << sessionid << std::endl;
                }

            }
            else
            {
                std::string sessionid = req.GetSessionId();
                    // std::cout << "用户id值: " << sessionid << std::endl;


                if (!sessionid.empty()) //如果不为空
                {
                    // std::cout << "用户id值: " << sessionid << std::endl;
                    // std::cout << "manager addr: " << &_manager << std::endl;
                    session_ptr user = _manager->GetSession(sessionid);
                    //问题 ： 为什么这里的user是空指针? 上面都已经 _manager->AddSession(s);
                    if (user != nullptr)
                    {
                        std::cout << "用户: " << user->_username << "正在活跃"<< std::endl;
                    }
                    else 
                    {
                        std::cout << "用户: " << sessionid << "已过期"<< std::endl;
                    }
      
                }
            }
            //任何连接来了都返回下面的网页
            res.SetCode(200);
            res.AddHeader("Content-Type: text/html");
            std::string body = "<html><body><p>Hello,World!</p></body></html>"; // 硬编码，无论访问什么路径都返回hello world
            res.SetBody(body);

            // 认为到现在过来的是一个完整的报文，不进行读取不完善处理
            std::string returnstr;
            res.Serialize(&returnstr);
            // std::cout << "--------------------------------" << std::endl;
            // std::cout << returnstr << std::endl;
            // std::cout << "--------------------------------" << std::endl;

            ret->TcpSend(returnstr);
        }

        ~HttpServer() { LOG(LogLevel::DEBUG) << "孙子进程销毁了，我没了????";}

    private:
        std::shared_ptr<TcpServer> _tsvr;
        SessionManager* _manager;
    };
}
